package com.ruoyi.business.config;

import com.ruoyi.business.listener.CompleteTaskListener;
import com.ruoyi.business.listener.ProcessCompleteListener;
import com.ruoyi.business.listener.TaskCreateListener;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Flowable 全局监听器注册
 *
 * @author zyb
 */
@Configuration
@RequiredArgsConstructor
public class FlowableGlobalListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final SpringProcessEngineConfiguration configuration;

    private final TaskCreateListener taskCreateListener;
    private final ProcessCompleteListener processCompleteListener;
    private final CompleteTaskListener completeTaskListener;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        // 注册 任务创建时的 监听器taskCreateListener。
//        dispatcher.addEventListener(taskCreateListener, FlowableEngineEventType.TASK_CREATED);
        // 注册 流程结束时的 监听器
//        dispatcher.addEventListener(processCompleteListener, FlowableEngineEventType.PROCESS_COMPLETED);
        // 注册 任务完成时的 监听器
        dispatcher.addEventListener(completeTaskListener, FlowableEngineEventType.TASK_COMPLETED);

    }
}
